<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="排序号" prop="sortNo">
						<el-input-number v-model="form.sortNo" placeholder="排序号" clearable></el-input-number>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="报表标题" prop="title">
						<el-input v-model="form.title" placeholder="报表标题" maxlength="40" clearable></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="所属分类" prop="biExcelSort">
						<el-cascader v-model="form.biExcelSort" :options="excelSort" :props="excelSortProps" :show-all-levels="false" clearable></el-cascader>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="是否启用" prop="status">
						<el-radio-group v-model="form.status">
							<el-radio-button label="0">停用</el-radio-button>
							<el-radio-button label="1">启用</el-radio-button>
						</el-radio-group>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="开始时间" prop="beginTime">
						<el-date-picker v-model="form.beginTime" placeholder="开始时间" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="结束时间" prop="endTime">
						<el-date-picker v-model="form.endTime" placeholder="结束时间" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="人员权限" prop="userRole">
				<select-user-dialog v-model:accountId="form.userRole" :mode="true"/>
			</el-form-item>
			<el-form-item label="部门权限">
				<select-dept-dialog v-model:deptId="form.deptRole" :mode="true"/>
			</el-form-item>
			<el-form-item label="职务权限">
				<select-user-level-dialog v-model:levelId="form.levelRole" :mode="true"/>
			</el-form-item>
			<el-form-item label="备注说明">
				<el-input v-model="form.remark" type="textarea" placeholder="备注说明" maxlength="180" clearable></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>

</template>
<script>
import selectUserDialog from "@/views/module/selectUserDialog";
import selectDeptDialog from "@/views/module/selectDeptDialog";
import selectUserLevelDialog from "@/views/module/selectUserLevelDialog";

export default {
	emits: ['success', 'closed'],
	components: {
		selectUserLevelDialog,
		selectUserDialog,
		selectDeptDialog,
	},
	data() {
		return {
			visible: false,
			isSaveing: false,
			isShowMsg: false,
			mode: "add",
			titleMap: {
				add: '创建Excel报表',
				edit: '编辑Excel报表',
			},
			excelSort: [],
			excelSortProps: {
				value: "sortId",
				label: "name",
				emitPath: false,
				checkStrictly: true
			},
			form: {
				manageId: '',
				sortNo: 0,
				title: '',
				biExcelSort: '',
				status: '0',
				beginTime:'',
				endTime:'',
				userRole:'',
				deptRole:'',
				levelRole:'',
				remark: '',
			},
			options: [{
				value: '0',
				label: '请选择'
			}],
			rules: {
				title: [
					{required: true, message: '报表标题不能为空', trigger: 'blur'}
				],
				biExcelSort: [
					{required: true, message: '所属分类不能为空', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
		this.getBiExcelSortTree();
	},
	methods: {
		async getBiExcelSortTree() {
			var res = await this.$API.bi.excel.getBiExcelSortTree.get();
			this.excelSort = res.data
		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.bi.excel.insertBiExcelManage.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.bi.excel.updateBiExcelManage.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		setData(data) {
			this.form.manageId = data.manageId
			this.form.sortNo = data.sortNo
			this.form.title = data.title
			this.form.biExcelSort = data.biExcelSort
			this.form.status = data.status
			this.form.beginTime = data.beginTime
			this.form.endTime = data.endTime
			this.form.userRole = data.userRole
			this.form.deptRole = data.deptRole
			this.form.levelRole = data.levelRole
			this.form.remark = data.remark
		},
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
